package com.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bean.Facilities;
import com.service.FacilitiesService;
import com.service.FacilitiesServiceImpl;


/**
 * Servlet implementation class FacilitiesServlet 
 */
@WebServlet("/FacilitiesServlet")
public class FacilitiesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FacilitiesServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");
		FacilitiesService service = new FacilitiesServiceImpl();
		if(action==null||"".equals(action)||"list".equals(action)){
			Facilities facilities = new Facilities();
			List<Facilities> list =  service.getList(facilities);
			request.setAttribute("myList", list); 
			request.getRequestDispatcher("list.jsp").forward(request, response);
		}
		if("add".equals(action)){
			String partsId = request.getParameter("partsId");
			String partsName = request.getParameter("partsName");
			String mileageNo = request.getParameter("mileageNo");
			String statusDesc = request.getParameter("statusDesc");
			Facilities f = new Facilities();
			f.setPartsId(Integer.getInteger(partsId));
			f.setPartsName(partsName);
			f.setMileageNo(Integer.getInteger(mileageNo));
			f.setStatusDesc(statusDesc);
			service.add(f);
			response.sendRedirect("FacilitiesServlet");
		}
		if("del".equals(action)){
			String id=request.getParameter("id");
			service.delete(Integer.parseInt(id));
			response.sendRedirect("FacilitiesServlet");
		}
		if("update".equals(action)){
			String partsId = request.getParameter("partsId");
			String partsName = request.getParameter("partsName");
			String mileageNo = request.getParameter("mileageNo");
			String statusDesc = request.getParameter("statusDesc");
			String routeId = request.getParameter("routeId");
			Facilities f = new Facilities();
			f.setPartsId(Integer.getInteger(partsId));
			f.setPartsName(partsName);
			f.setMileageNo(Integer.getInteger(mileageNo));
			f.setStatusDesc(statusDesc);
			f.setRouteId(Integer.parseInt(routeId));
			service.update(f);
			request.getRequestDispatcher("update.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
